import javax.management.ObjectName;

/*
*Description:
*User:laoHu
*Created with Intellij IDEA 2025
*Data:2025.06.30
*Time:21:07
*/
//实现泛型的继承
class Alg<E extends Comparable<E>>{
    public E maxVal(E[] nums){
        E max = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if(max.compareTo(nums[i]) < 0) max = nums[i];
        }
        return max;
    }
}
class Alg1{
    public <E extends Comparable<E>> E maxVal(E[] nums){
        E max = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if(max.compareTo(nums[i]) < 0) max = nums[i];
        }
        return max;
    }
}
class Alg2{
    public static <E extends Comparable<E>> E maxVal(E[] nums){
        E max = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if(max.compareTo(nums[i]) < 0) max = nums[i];
        }
        return max;
    }
}
//泛型的上界：E的上界是Number和其子类
class Test<E extends Number>{}
//泛型的使用
class myArray<E>{
    Object[] objects = new Object[10];
    public void setVal(int pos,E val){
        objects[0] = val;
    }
    public E getVal(int pos){
        return (E)objects[pos];//E是子类，需要强制类型转换
    }
}
public class Main{
    //泛型方法：不实例化对象，使用静态方法
    public static void main(String[] args) {
        Integer[] integers = {1,2};
        int r = Alg2.maxVal(integers);
        System.out.println(r);
    }
    //泛型方法：实例化对象
    public static void main6(String[] args) {
        Alg1 alg1 = new Alg1();
        Integer[] integers = {1,2,3};
        //方法前的包装类可写可不写，建议写
        int r = alg1.<Integer>maxVal(integers);
        System.out.println(r);
    }
    //泛型上界的使用
    public static void main5(String[] args) {
        Alg<Integer> alg = new Alg<>();
        Integer[] nums = {1,2,3,4,5};
        int r = alg.maxVal(nums);
        System.out.println(r);
    }
    //泛型的上界
    public static void main4(String[] args) {
        Test<Integer> test = new Test<>();//Interger是Number的子类，可以实例
        /*Test<String> test1 = new Test<String>();*///报错，String不是Number子类
    }

    //阿里面试题
    public static void main3(String[] args) {
        Integer a = 100;
        Integer b = 100;
        System.out.println(a == b);
        System.out.println("=====");
        Integer c = 200;
        Integer d = 200;
        System.out.println(c == b);
    }

    //拆箱：将包装类类型转换为基本数据类型
    public static void main2(String[] args) {
        Integer i = 10;
        int a = i.intValue();//显示拆箱
        int b = i;//隐式拆箱,底层调用intValue()方法
        double d = 10;
        double c = i.doubleValue();
    }

    //装箱：将基本数据类型转换为包装类类型
    public static void main1(String[] args) {
        int a = 10;
        Integer i = Integer.valueOf(a);//显示装箱
        Integer j = 10;//隐式装箱，底层自动转换，调用Integer.valueOf()方法
    }
}
